from datetime import datetime
from enum import Enum
from typing import Dict, List, Optional

from odmantic import Model, EmbeddedModel
from odmantic import Field


class Signup(Model):
    id: int = Field(..., primary_field=True)
    username: str = Field(..., unique=True)
    password: str = Field(...)


class Gender(str, Enum):
    MALE = u"男性"
    FEMALE = u"女性"


class Education(str, Enum):
    HIGHER = u"高等教育"
    SECONDARY = u"中等教育"
    PRIMARY = u"初等教育"
    SPECIAL = u"特殊教育"
    PRESCHOOL = u"学前教育"
    ILLITERATE = u"未受教育"


class Permission(str, Enum):
    SUPERIOR = "superior"
    ADMIN_WRITE = "admin_write"
    ADMIN_READ = "admin_read"
    USER = "user"
    GUEST = "guest"


class Position(str, Enum):
    PUBLIC_SERVANT = u"公职人员"
    TEACHER = u"教师"
    MILITARY_PERSONNEL = u"军人"
    STUDENT = u"学生"
    EMPLOYEE = u"企业员工"
    FREELANCE = u"自由职业者"


class PsychoTest(EmbeddedModel):
    id: int = Field(..., unique=True)
    test_date: datetime = Field(...)
    test_name: str = Field(...)
    answers: Dict = Field(...)
    scores: Dict = Field(...)


class PMH(EmbeddedModel):
    id: int = Field(..., unique=True)
    pmh_date: datetime = Field(...)
    symptoms: str = Field(...)
    diagnosis: str = Field(...)


class FMH(EmbeddedModel):
    id: int = Field(..., unique=True)
    relations: str = Field(...)
    symptoms: str = Field(...)
    diagnosis: str = Field(...)


class PsychoTherapy(EmbeddedModel):
    id: int = Field(..., unique=True)
    psycho_therapy_date: datetime = Field(...)
    psycho_therapy: str = Field(...)
    duration: str = Field(...)


class Medication(EmbeddedModel):
    id: int = Field(..., unique=True)
    medication_date: datetime = Field(...)
    medications: str = Field(...)
    duration: str = Field(...)


class LifeEvent(EmbeddedModel):
    id: int = Field(..., unique=True)
    life_event_date: datetime = Field(...)
    life_events: str = Field(...)
    duration: str = Field(...)


class Intervetion(EmbeddedModel):
    id: int = Field(..., unique=True)
    intervention_date: datetime = Field(...)
    interventions: str = Field(...)
    duration: str = Field(...)


class Profile(EmbeddedModel):
    fullname: Optional[str] = None
    gender: Optional[Gender] = None
    age: Optional[int] = None
    education: Optional[Education] = None
    position: Optional[Position] = None
    psycho_tests: List[PsychoTest] = []
    personal_medical_history: List[PMH] = []
    family_medical_history: List[FMH] = []
    psycho_therapy: List[PsychoTherapy] = []
    medications: List[Medication] = []
    life_events: List[LifeEvent] = []
    interventions: List[Intervetion] = []


class Login(Model):
    id: int = Field(..., primary_field=True)
    username: str = Field(..., unique=True)
    password: str = Field(...)
    passphrase: str = Field(...)
    approved_date: datetime = Field(...)
    permission_sets: list[Permission] = ['user']
    profiles: Profile = Profile()


if __name__ == "__main__":
    # test Signup
    details = {'id': 1, 'username': 'skunk69', 'password': '123456'}
    signup = Signup(**details)
    print('----test Signup----')
    print(f'signup: {signup}')

    # test Login
    details = {'id': 1, 'username': 'skunk69', 'password': '123456',
               'passphrase': 'xxxxx', 'approved_date': datetime.today()}
    login = Login(**details)
    print('----test Login----')
    print(f'login: {login}')

    # test Login+Profile
    psycho_tests = PsychoTest(id=1, test_date=datetime.today(),
                              test_name='SCL_90', answers={}, scores={})
    details = {'login_id': 1, 'fullname': 'Tigger Smith',
               'gender': Gender.MALE.value, 'age': 16,
               'education': Education.SECONDARY.value,
               'position': Position.STUDENT.value,
               'psycho_tests': [psycho_tests]}

    profiles = Profile(**details)
    permission_sets = [Permission.USER.value,
                       Permission.ADMIN_READ.value,
                       Permission.ADMIN_WRITE.value,
                       Permission.SUPERIOR.value]

    details = {'id': 1, 'username': 'skunk69', 'password': '123456',
               'passphrase': 'xxxxx', 'approved_date': datetime.today(),
               'profiles': profiles, 'permission_sets': permission_sets}
    login = Login(**details)
    print('----test Login+Profile----')
    print(f'login: {login}')
    print(f'education={login.profiles.education}')
    print(f'psycho_tests={login.profiles.psycho_tests[0].test_date}')
    print(f'life_events={login.profiles.life_events}')
    print(f'login.permission_sets= {login.permission_sets}')
